package dth.dao;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import dth.entity.PaymentHistory;


public class PaymentHistoryDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	
	public java.util.List<PaymentHistory> getListPaymentHistory() {
		session.beginTransaction();
		String queryString = "Select * from  payment_history";
		SQLQuery query=session.createSQLQuery(queryString).addEntity(PaymentHistory.class);
		return query.list();
	} 
	public java.util.List<PaymentHistory> getListPaymentHistoryByUserId(int id) {
		session.beginTransaction();
		String queryString = "Select * from  payment_history where fk_payment_user=:id order by date_insert";
		SQLQuery query=session.createSQLQuery(queryString).addEntity(PaymentHistory.class);
		query.setParameter("id",id);
		return query.list();
	}
	public int insert(PaymentHistory pay) {
		Transaction tx=session.beginTransaction();
		String queryString = "Insert into payment_history(date_insert,payment_amount,fk_payment_user) values(:date_insert,:payment_amount,:fk_payment_user) ";
		SQLQuery query = session.createSQLQuery(queryString);
		query.setParameter("date_insert", pay.getDateInsert());
		query.setParameter("payment_amount", pay.getPrice());
		query.setParameter("fk_payment_user", pay.getFkPaymentUser());
		System.out.println("insert Pay with id user: "+pay.getFkPaymentUser());
		int count = query.executeUpdate();
		tx.commit();
		return count;
	}
	
	public int delete(int id)
	{
		Transaction tx = session.beginTransaction();
		String queryString = "Delete  from payment_history where payment_id=:id";
		SQLQuery query = session.createSQLQuery(queryString);
		query.setParameter("id", id);

		int count = query.executeUpdate();
		tx.commit();
		return count;
	}
}
